package com.zdj.websocket.service;

import com.zdj.websocket.model.InMessage;
import com.zdj.websocket.model.OutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * @author zhangdj
 */
@Service
public class WebSocketService {

    @Autowired
    private SimpMessagingTemplate template;

    public void send(String dest, String content) {
        for (int i = 0; i < 10; i++) {
            template.convertAndSend(dest, new OutMessage(content + i));
        }
    }

    public void sendSingle(String dest, InMessage inMessage) {
        template.convertAndSend(dest, new OutMessage("来自：" + inMessage.getFrom() + ":" + inMessage.getContent()));
    }

    public void sendServerInfo(String dest) {
        template.convertAndSend(dest, new OutMessage("可用内存为" + Runtime.getRuntime().freeMemory()));
    }
}
